package com.bnz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 权限拦截器
 */
public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.1 得到session对象
        HttpSession session = request.getSession();
        //1.2 从session中得到用户名
        Object username = session.getAttribute("username");
        //1.3 判断，如果用户名不存在，也跳转到登录页面，如果存在放行
        if(username == null){
            response.sendRedirect("/login.jsp");
            return false;
        }
        //1.4 如果存在就放行
        return true;
    }
}
